#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>

void * func(void * arg){
    //采用静态变量，延长num的周期为进程退出才回收
    static int num = 100;
    return &num;
}

void * func2(void * arg){
    //这一步是修改外部传过来的值
    int* int_p = (int*)arg;
    (*int_p) = 1024;
    //这一步是构造返回值
    int* ret_p = (int*)malloc(sizeof(int));
    (*ret_p) = 1024;
    return ret_p;
}

int main(){
    pthread_t tid;
    int res  = pthread_create(&tid, NULL, func, NULL);
    if(res){
        perror("pthread_create error");
        exit(1);
    }

    void * ret;
    res = pthread_join(tid, &ret);
    if(res){
        perror("pthread_join error");
        exit(1);
    }

    printf("ret = %d\n", *(int *)ret);

    int num2 = 101;
    pthread_t tid2;
    int res2 = pthread_create(&tid2, NULL, func2, &num2);
    if(res2){
        perror("pthread_create error");
        exit(1);
    }

    int * ret2 = NULL;
    res2 = pthread_join(tid2, (void**)&ret2);
    if(res2){
        perror("pthread_join error");
        exit(1);
    }

    printf("ret2 = %d\n", *ret2);
    free(ret2);
    return 0;
}